import encoders

####### SUPERCLASS

class payload:
	def __init__(self):
		self.count=0
		self.appended=None

	def __iter__ (self):
		it=self.owniter()

		if self.appended!=None:
			it.append(self.appended)
		return it

	def owniter (self):
		return payload_iterator()

	def __len__(self):
		if self.appended!=None:
			return self.count*len(self.appended)
		return self.count

	def append(self,payload):
		self.appended=payload
		pass


class payload_iterator:
	def __init__(self):
		self.appended=None
		self.appenditerator=None
		self.actual=None

	def next (self):
		if self.appended!=None:
			try:
				apend=self.appenditerator.next()
				return self.actual+apend
			except StopIteration:
				self.actual=self.do_next()
				self.appenditerator=self.appended.__iter__()
				return self.next()

		else:
			return self.do_next()

	def do_next(self):
		raise StopIteration

	def append(self,payload):
		self.actual=self.do_next()
		self.appended=payload
		self.appenditerator=payload.__iter__()
	

######################################
######################################
######## Inheritances
######################################
######################################

class payload_file (payload):
	def __init__(self,file):
		payload.__init__(self)
		self.file=file
		f=open(self.file)
		self.count=len(f.readlines())
		f.close()

	def owniter (self):
		f=open(self.file)
		return file_iterator(f)

class file_iterator (payload_iterator):
	def __init__(self,file):
		payload_iterator.__init__(self)
		self.file=file
		
	def do_next (self):
		try:
			line=self.file.next().strip()
			return line
		except StopIteration:
			self.file.close()
			raise StopIteration

###################Payload from Paygen

class payload_payload (payload):
	def __init__(self,dict):
		payload.__init__(self)
		self.count=len(dict)
		self.dict=dict
		self.dict.reverse()
		
	def owniter (self):
		return pay_iterator(self.dict)

class pay_iterator (payload_iterator):
	def __init__(self,dict):
		payload_iterator.__init__(self)
		self.dict=dict

	def do_next (self):
		line=self.dict.pop()
		return line


################### RANGE PAYLOAD


class payload_range (payload):
	def __init__(self,range,width=0):    ## range example --> "23-56"
		payload.__init__(self)
		try:
			ran=range.split("-")
			self.minimum=int(ran[0])
			self.maximum=int(ran[1])
			self.count=self.maximum - self.minimum + 1
			self.width=width
		except:
			raise Exception, "Bad range format (eg. \"23-56\")"
		

	def owniter (self):
		return range_iterator(self.minimum,self.maximum,self.width)


class range_iterator (payload_iterator):
	def __init__(self,min,max,width):
		payload_iterator.__init__(self)
		self.minimum=min
		self.maximum=max
		self.width=width
		self.current=self.minimum
		
	def do_next (self):
		if self.current>self.maximum:
			raise StopIteration
		if self.width:
			payl="%0"+str(self.width)+"d"
			payl=payl % (self.current)
		else:
			payl=str(self.current)

		self.current+=1
		return payl
	

################### HEXRANGE PAYLOAD


class payload_hexrange (payload):
	def __init__(self,range):    ## range example --> "0-ffa"
		payload.__init__(self)
		try:
			ran=range.split("-")
			self.minimum=int(ran[0],16)
			self.maximum=int(ran[1],16)
			self.count=self.maximum - self.minimum
		except:
			raise Exception, "Bad range format (eg. \"0-ffa\")"
		
	def owniter (self):
		return hexrange_iterator(self.minimum,self.maximum)

class hexrange_iterator (payload_iterator):
	def __init__(self,min,max):
		payload_iterator.__init__(self)
		self.minimum=min
		self.maximum=max
		self.current=self.minimum
		
	def do_next (self):
		if self.current>self.maximum:
			raise StopIteration
		
		lgth=len(hex(self.maximum).replace("0x",""))
		pl="%"+str(lgth)+"s"
		num=hex(self.current).replace("0x","")	
		pl= pl % (num)
		payl="%"+pl.replace(" ","0")
		
		self.current+=1

		return payl
		
		
######################### PAYLOAD LIST


class payload_list (payload):
	def __init__(self,list):   
		payload.__init__(self)
		self.list=list
		self.count=len(list)
		
	def owniter (self):
		return plist_iterator(self.list)


class plist_iterator (payload_iterator):
	def __init__(self,list):
		payload_iterator.__init__(self)
		self.list=list
		self.current=0
		
	def do_next (self):
		try:
			elem=self.list[self.current]
			self.current+=1
			return elem
		except:
			raise StopIteration
		

if __name__=="__main__":
	a=payload_list(["hola","adios","pepe"])
	a.append(payload_list([".html",".rar"]))
	for i in a:
		print i
